package com.other.springbootconfigureorder.data;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class MockDataRunner implements ApplicationRunner {
    @Autowired
    ExcelReader excelReader;
    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<List<String>> featureList = excelReader.readExcelData("配置表");
        Map<String, Map<String, String>> dataMap = new HashMap<>();
        Map<String, String> gs001Map = new HashMap<>();
        Map<String, String> gs002Map = new HashMap<>();
        Map<String, String> gs003Map = new HashMap<>();
        Map<String, String> gs004Map = new HashMap<>();
        dataMap.put("GS001", gs001Map);
        dataMap.put("GS002", gs002Map);
        dataMap.put("GS003", gs003Map);
        dataMap.put("GS004", gs004Map);

        System.out.println(featureList);
        featureList.stream().skip(1).forEach(
                strList -> {
                    String featureCode = strList.get(2);
                    gs001Map.put(featureCode, strList.get(3));
                    gs002Map.put(featureCode, strList.get(4));
                    gs003Map.put(featureCode, strList.get(5));
                    gs004Map.put(featureCode, strList.get(6));
                }
        );

        // 序列化并保存到文件
        String filename = "mapData.json";
        objectMapper.writeValue(new File(filename), dataMap);


        // 从文件反序列化
        Map<String, Map> restoredData = objectMapper.readValue(new File(filename), new TypeReference<Map<String, Map>>() {
        });

        // 验证结果
        System.out.println("Restored data: " + restoredData.toString());
    }
}
